local dir `c(pwd)'
local g=`1'
qui {
clear
set obs 1
foreach type in original order structure {
gen entropy_`type'=.
gen words_`type'=.
gen chars_`type'=.
}
gen book=.             
gen str translation=""	                        
save  entropy_bible_`g', replace

use calculation_`g', clear

local till=_N
local counter=_N

forvalues i=1/`till' {
	use calculation_`g', clear
	local translation=translation[`i']
	local counter=`counter'-1
	use "`dir'\\corpus\\`translation'.dta" , clear
	drop if sentence==""
	
	/* get bible books */
	gen book=real(usubstr(vid,1,2))
	drop if book==.
	keep book
	
	/* create macro containing each available book */

	gen match=1
	collapse (sum) match, by(book)
	local to=_N
	local books
	forvalues b=1/`to' {
		local add=book[`b']
		local books `"`books' `add'"'
		}	
	noisily di "`translation' (left: `counter') "
    	
	foreach book of local books		{			
		foreach type in original order structure {					
			import delimited `"`dir'\\shortestmismatches\\`translation'_`book'_`type'.txt.sm.txt"', delimiter(tab) varnames(nonames) stripquote(no) bindquotes(nobind) case(preserve) encoding(UTF-8) clear  
			save `"`dir'\\stata_files\length_`translation'_`book'_`type'"', replace 
			local chars_`type'_`book'=_N
			gen words=1 if v1==" "
			sum words if words==1
			local words_`type'_`book'=r(N)
			ren v2 li
			gen i=_n
			local n=_N
			gen tosum=(li)/(log(i)/log(2))
			sum tosum
			local entropy_`type'_`book'=r(sum)
		}
	}
	
	foreach book of local books		{			
		use  entropy_bible_`g', clear
					local new=_N+1
					set obs `new'
					local in `"in `new'"'
					foreach type in original order structure {
							replace entropy_`type'=`entropy_`type'_`book'' 	`in'
							replace chars_`type'=`chars_`type'_`book'' 			`in'
							replace words_`type'=`words_`type'_`book'' 			`in'
							}   
					replace book=`book'                     			`in'      		
					replace translation="`translation'"	          	`in'  
					save  entropy_bible_`g', replace
				}
			}
/* generate finished file */
clear
set obs 1
gen v=1
save finished_`g', replace
}

clear         
exit, STATA 	
